package com.relytosoft.mizar.media.analyze.services.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.relytosoft.dubhe.common.core.exception.ServiceException;
import com.relytosoft.mizar.media.analyze.api.constant.VehicleType;
import com.relytosoft.mizar.media.analyze.api.entity.system.AyVehicleGroup;
import com.relytosoft.mizar.media.analyze.api.entity.system.AyVehicleInfo;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.VehicleInfoImportVo;
import com.relytosoft.mizar.media.analyze.services.service.system.AyVehicleGroupService;
import com.relytosoft.mizar.media.analyze.services.service.system.AyVehicleInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 创建读取excel监听器
 *
 * @author lj
 * @date 2022/5/23 17:43
 */

public class VehicleInfoExcelListener extends AnalysisEventListener<VehicleInfoImportVo> {

    private AyVehicleInfoService ayVehicleInfoService;
    private List<Integer> ids;
    private AyVehicleGroupService ayVehicleGroupService;


    public VehicleInfoExcelListener() {
    }

    public VehicleInfoExcelListener(AyVehicleInfoService ayVehicleInfoService, List<Integer> ids, AyVehicleGroupService ayVehicleGroupService) {
        this.ayVehicleInfoService = ayVehicleInfoService;
        this.ids = ids;
        this.ayVehicleGroupService = ayVehicleGroupService;
    }

    /**
     * 一行一行去读取excle内容
     *
     * @param vehicleInfoImportVo 需要导入的信息
     * @param analysisContext     参数
     */
    @Override
    public void invoke(VehicleInfoImportVo vehicleInfoImportVo, AnalysisContext analysisContext) {
        if (vehicleInfoImportVo == null) {
            throw new ServiceException("文件中没有数据(⊙︿⊙)");
        }
        //保存车辆信息
        AyVehicleInfo ayVehicleInfo = new AyVehicleInfo();
        ayVehicleInfo.setVehicleNumber(vehicleInfoImportVo.getVehicleNumber());
        ayVehicleInfo.setRemark(vehicleInfoImportVo.getRemark());
        ayVehicleInfo.setVehicleType(getType(vehicleInfoImportVo.getVehicleType()));
        ayVehicleInfo.setStatus(1);
        ayVehicleInfoService.save(ayVehicleInfo);

        //保存关联信息
        List<AyVehicleGroup> ayVehicleGroupList = new ArrayList<>();
        ids.forEach(id -> {
            AyVehicleGroup group = new AyVehicleGroup();
            group.setVehicleId(ayVehicleInfo.getId());
            group.setGroupId(id);
            ayVehicleGroupList.add(group);
        });
        if (ayVehicleGroupList.size() > 0) {
            ayVehicleGroupService.saveBatch(ayVehicleGroupList);
        }
    }

    public Integer getType(String type) {
        VehicleType[] values = VehicleType.values();
        for (VehicleType value : values) {
            if (value.getRemark().equals(type.trim())) {
                return Integer.valueOf(value.getValue());
            }
        }
        return null;
    }

    /**
     * 读取excel表头信息
     *
     * @param headMap 表头信息
     * @param context 上下文
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头信息：" + headMap);
    }

    /**
     * 读取完成后执行
     *
     * @param analysisContext 执行
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }
}
